package ext.tianma.part.finishedProduct.builder;


import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import wt.csm.navigation.ClassificationNode;
import wt.csm.navigation.litenavigation.ClassificationNodeDefaultView;
import wt.csm.navigation.service.ClassificationObjectsFactory;
import wt.log4j.LogR;
import wt.project.Role;
import wt.util.WTException;

import com.ptc.core.ui.resources.ComponentMode;
import com.ptc.core.ui.resources.ComponentType;
import com.ptc.jca.mvc.components.AbstractAttributesComponentBuilder;
import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.mvc.components.AttributeConfig;
import com.ptc.mvc.components.AttributePanelConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfigFactory;
import com.ptc.mvc.components.ComponentId;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.components.CustomizableViewConfig;
import com.ptc.mvc.components.GroupConfig;
import com.ptc.netmarkets.util.beans.NmHelperBean;

import ext.com.core.CoreUtil;
import ext.tianma.part.finishedProduct.util.CSMUtil;
import ext.tianma.part.finishedProduct.util.LoadAttrsConfig;
import ext.tianma.util.PropertiesUtil;

@ComponentBuilder("finishedGood.builder.PMReleaseFinishedGoodBuilder")
public class PMReleaseFinishedGoodBuilder extends AbstractAttributesComponentBuilder {
	private static Logger log = LogR.getLogger(PMReleaseFinishedGoodBuilder.class.getName());

	@Override
	protected CustomizableViewConfig buildAttributesComponentConfig(
			ComponentParams paramComponentParams) throws WTException {
		log.debug("PMReleaseFinishedGoodBuilder::start");
		NmHelperBean nmhelperbean = ((JcaComponentParams)paramComponentParams).getHelperBean();
		ComponentMode localComponentMode = getComponentMode(paramComponentParams);
		ComponentConfigFactory factory = getComponentConfigFactory();
				
		AttributePanelConfig panelConfig = factory.newAttributePanelConfig(ComponentId.ATTRIBUTE_PANEL_ID);
		panelConfig.setComponentMode(localComponentMode);
		panelConfig.setComponentType(ComponentType.WIZARD_ATTRIBUTES_TABLE);

		String classNodeOid = String.valueOf(nmhelperbean.getRequest().getParameter("part_classify_hidden"));
		if(null == classNodeOid || StringUtils.isEmpty(classNodeOid)){
			return panelConfig;
		}
		ClassificationNode classNode = (ClassificationNode) CoreUtil.getWTObjectByOid(classNodeOid);
		ClassificationNodeDefaultView cndv = ClassificationObjectsFactory.newClassificationNodeDefaultView(classNode);
		String classNodeFullPath = CSMUtil.getClassificationFullPath(cndv);//"MDM-成品/非显示模组/测试2"
		
		GroupConfig group = factory.newGroupConfig();
		group.setId("partAttrs.edit.id");
		group.setLabel("成品类型属性");
		group.setComponentMode(ComponentMode.EDIT);
		
		GroupConfig classAttrGroup = factory.newGroupConfig();
		classAttrGroup.setId("partClassIBAs.edit.id");
		classAttrGroup.setLabel("分类属性");
		classAttrGroup.setComponentMode(ComponentMode.EDIT);
		
		//获取属性配置信息;
		List<String> finishedGoodDimensionList = (List<String>) nmhelperbean.getNmCommandBean().getComboBox().get("finishedGoodDimension");
		String finishedGoodDimension = finishedGoodDimensionList.get(0);
		String roleName = "PM";
		String editAttrs = "";
		try {
			editAttrs = PropertiesUtil.getValueByKey(classNodeFullPath + "."  + finishedGoodDimension + "." + roleName + ".EDIT", LoadAttrsConfig.configFilePath);
			log.debug("editAttrs==>>"+editAttrs);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//动态生成编辑组件
		if(StringUtils.isNotEmpty(editAttrs)){
			//获取分类节点下所有属性   Map<内部名称,显示名称>
		    Map<String, String> classNodeAllIBAs = CSMUtil.getClassNodeAllIBAs(classNode);
		    String[] attrArry = editAttrs.split(";");
		    boolean isHasClassAttrs = false;
		    boolean isHasPdmAttrs = false;
			for(String attrInternalName : attrArry){
				if(classNodeAllIBAs.containsKey(attrInternalName)){
					//分类属性
					classAttrGroup.addComponent(newAttribute(attrInternalName,attrInternalName, factory,true,"2"));
					isHasClassAttrs = true;
				}else{
					//PDM属性
					group.addComponent(newAttribute(attrInternalName,attrInternalName, factory,true,"1"));
					isHasPdmAttrs = true;
				}
			}
			if(isHasPdmAttrs){
				panelConfig.addComponent(group);
			}
			
			if(isHasClassAttrs){
				panelConfig.addComponent(classAttrGroup);
			}
		}
		
		//动态生成选人组件
		List<String> roleNameList = LoadAttrsConfig.getMaintainRolesName(classNodeFullPath,finishedGoodDimension);
		log.debug("roleNameList==>>"+roleNameList);
		GroupConfig viewGroup = factory.newGroupConfig();
		viewGroup.setId("users.select.id");
		viewGroup.setLabel("人员维护");
		if(null != roleNameList && roleNameList.size() > 0){
			viewGroup.setComponentMode(ComponentMode.VIEW);
			for(String roleNameTemp : roleNameList){
				if("PM".equals(roleNameTemp)){
    				continue;
    			}
				Role role = Role.toRole(roleNameTemp);
				viewGroup.addComponent(newAttribute(roleNameTemp,role.getDisplay(Locale.CHINA), factory,true,"3"));
			}
		}
		//BOMAssessor.value=BOM审核者
		//Role role1 = Role.toRole("BOMSigner");
		//viewGroup.addComponent(newAttribute("BOMSigner",role1.getDisplay(Locale.CHINA), factory,true,"3"));
		panelConfig.addComponent(viewGroup);
		return panelConfig;
	}
	
	private static AttributeConfig newAttribute(String id, String label ,ComponentConfigFactory factory,boolean isEdit,String type){
		AttributeConfig attribute = factory.newAttributeConfig();
		attribute.setId(id);
		if(isEdit){
			if("1".equals(type)){
				attribute.setDataUtilityId("partIBAsDataUtility.id");
			}else if("2".equals(type)){
				attribute.setDataUtilityId("classIBAsDataUtility.id");
			}else if("3".equals(type)){
				attribute.setLabel(label);
				attribute.setDataUtilityId("userPickDataUtility.id");
			}
		}
		return attribute;
	}
}
